package com.lei.yohuo001java.presenter;

import android.util.Log;
import android.view.View;

import com.lei.mvp_retrofitlibrary.http.bean.GoodsBean;
import com.lei.mvp_retrofitlibrary.http.bean.LoginBean;
import com.lei.mvp_retrofitlibrary.http.bean.RegisterBean;
import com.lei.mvp_retrofitlibrary.http.observer.BaseObservable;
import com.lei.mvp_retrofitlibrary.http.observer.BaseObserver;
import com.lei.mvp_retrofitlibrary.mvp.presenter.BasePresenter;
import com.lei.mvp_retrofitlibrary.mvp.view.IView;
import com.lei.yohuo001java.LoginActivity;
import com.lei.yohuo001java.RegisterActivity;
import com.lei.yohuo001java.a_fragment.Fragment1;
import com.lei.yohuo001java.contract.ShopContract;
import com.lei.yohuo001java.fragmentyouhuo.FourFragment;
import com.lei.yohuo001java.fragmentyouhuo.TwoFragment;
import com.lei.yohuo001java.one_fragment.AFragment;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @ProjectName : YoHuo001Java
 * @Author : 雷金泷
 * @Time : 2020/12/17 9:28
 * @Description : 文件描述
 */
public class ShopPresenter extends BasePresenter<ShopContract.Model, IView> implements ShopContract.Presenter {
    private AFragment fragment1;
    private TwoFragment fragment01;
    private LoginActivity loginActivity;
    private RegisterActivity registerActivity;
    public ShopPresenter(ShopContract.Model mModel, ShopContract.View mmview) {
        super(mModel, mmview);
        fragment1 = (AFragment) mmview;
    }
    public ShopPresenter(ShopContract.Model mModel, ShopContract.ShopView mmview) {
        super(mModel, mmview);
        fragment01 = (TwoFragment) mmview;
    }
    public ShopPresenter(ShopContract.Model mModel, ShopContract.LoginView mmview) {
        super(mModel, mmview);
        loginActivity = (LoginActivity) mmview;
    }
    public ShopPresenter(ShopContract.Model mModel, ShopContract.RegisterView mmview) {
        super(mModel, mmview);
        registerActivity = (RegisterActivity) mmview;
    }
    @Override
    public void initShop(int page) {
        HashMap<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("pagesize","1000");
        new BaseObservable().doIt(mModel.initshop(map), new BaseObserver<GoodsBean>(){
            @Override
            public void onNext(GoodsBean goodsBean) {
                fragment1.initShop(goodsBean);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("ljl", "onError: "+e.getMessage() );
            }
        });
    }

    @Override
    public void initShop2(HashMap<String,String> map) {
        new BaseObservable().doIt(mModel.initshop(map), new BaseObserver<GoodsBean>(){
            @Override
            public void onNext(GoodsBean goodsBean) {
                if (goodsBean!=null){
                    fragment01.initShop2(goodsBean);
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("ljl", "onError: "+e.getMessage() );
            }
        });
    }

    @Override
    public void initlogin(String name,String pwd) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "{\n" +
                "  \"id\": 1,\n" +
                "  \"username\": \"" + name + "\",\n" +
                "  \"pwd\": \"" + pwd + "\",\n" +
                "  \"sex\": \"1\",\n" +
                "  \"birthday\": \"123\"\n" +
                "}");
        new BaseObservable().doIt(mModel.initlogin(requestBody), new BaseObserver<LoginBean>() {
            @Override
            public void onNext(LoginBean loginBean) {
                loginActivity.logindata(loginBean);
                FourFragment.tv1Four.setText("用户已登录");
                FourFragment.bt1Four.setVisibility(View.VISIBLE);
                FourFragment.bt2LogFour.setVisibility(View.GONE);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("ljl", "onError: "+e.getMessage() );
            }
        });
    }

    @Override
    public void initregister(String name,String pwd) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "{\n" +
                "  \"id\": 1,\n" +
                "  \"username\": \"" + name + "\",\n" +
                "  \"pwd\": \"" + pwd + "\",\n" +
                "  \"sex\": \"1\",\n" +
                "  \"birthday\": \"123\"\n" +
                "}");
        new BaseObservable().doIt(mModel.initregister(requestBody), new BaseObserver<RegisterBean>() {
            @Override
            public void onNext(RegisterBean registerBean) {
                registerActivity.registerData(registerBean);
            }

            @Override
            public void onError(Throwable e) {
                Log.e("ljl", "onError: "+e.getMessage() );
            }
        });
    }
}
